//引入样式
require("../../assets/css/common.css");
require("../../assets/font_44fp3ls0tzq/iconfont.css");
require("../../assets/css/reset.css");
require("../../assets/css/normalize.css");
require("./sports_running.less");
//引入模块
const dom = require("../../utils/dom.js");
const axios = require("../../utils/axios.js");
const date = require("../../utils/date.js");

document.addEventListener("DOMContentLoaded", function () {
	//1.计算距离
	var p1 = [116.498832, 39.975519];
	var p2 = [113.543076, 22.186927];
	// 返回 p1 到 p2 间的地面距离，单位：米
	var dis = AMap.GeometryUtil.distance(p1, p2);

	//获取随机数
	function getTime(max, min) {
		return Math.floor(Math.random() * (max - min + 1) + min);
	}
	//2.定时器模拟数据
	let duration = 0;
	let meter = 0;
	let calorie = 0;
	let time = null;
	function start() {
		time = setInterval(function () {
			//速度
			let speed = getTime(3, 8);
			dom.get("#speed").textContent = speed + "米/秒";
			//总时间
			duration++;
			dom.get("#time").textContent = date.getXHS(duration);

			//总公里数
			meter += speed;
			dom.get("#num").textContent = (meter / 1000).toFixed(2);

			//总千卡数
			calorie = 50 * (meter / 1000) * 1.036;
			dom.get("#calorie").textContent = calorie.toFixed(2);
		}, 800);
	}
	start();
	//暂停
	dom.get("#pause").addEventListener("click", function () {
		dom.get("#continue").style.display = "block";
		dom.get("#end").style.display = "block";
		dom.get("#pause").style.display = "none";
		clearInterval(time);
	});

	//开始
	dom.get("#continue").addEventListener("click", function () {
		dom.get("#continue").style.display = "none";
		dom.get("#end").style.display = "none";
		dom.get("#pause").style.display = "block";
		start();
	});
	//结束
	dom.get("#end").addEventListener("click", function () {
		//发送请求
		axios
			.post("/api/exercise/save", {
				type: 0,
				duration,
				calorie,
				meter,
			})
			.then((res) => {
				if (res.data.errno === 0) {
					location.href = "sports_run.html";
				}
			});
	});
});
